﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Maui.Controls;
using System.Windows.Input;

namespace ToDoApp
{
    public class TaskItem
    {
        public string Name { get; set; }
        public DateTime DueDate { get; set; }
        public string Priority { get; set; }
        public bool IsCompleted { get; set; }
    }

    public partial class MainPage : ContentPage
    {
        // ObservableCollection用于动态更新UI
        public ObservableCollection<TaskItem> Tasks { get; set; }
        public ObservableCollection<TaskItem> FilteredTasks { get; set; }

        public List<string> Priorities { get; set; }
        public string SelectedPriority { get; set; }

        // Commands
        public ICommand AddTaskCommand { get; }
        public ICommand DeleteTaskCommand { get; }
        public ICommand EditTaskCommand { get; }
        public ICommand SortByDateCommand { get; }
        public ICommand FilterCompletedCommand { get; }

        public MainPage()
        {
            InitializeComponent();

            // 初始化集合
            Tasks = new ObservableCollection<TaskItem>();
            FilteredTasks = new ObservableCollection<TaskItem>(Tasks);
            Priorities = new List<string> { "Low", "Medium", "High" };

            // 初始化命令
            AddTaskCommand = new Command(AddTask);
            DeleteTaskCommand = new Command<TaskItem>(DeleteTask);
            EditTaskCommand = new Command<TaskItem>(EditTask);
            SortByDateCommand = new Command(SortByDate);
            FilterCompletedCommand = new Command(FilterCompleted);

            // 将视图绑定到ViewModel
            BindingContext = this;
        }

        // 添加任务
        private void AddTask()
        {
            string taskName = TaskInput.Text;
            if (string.IsNullOrWhiteSpace(taskName))
                return;

            // 创建任务并添加到 Tasks 集合
            var newTask = new TaskItem
            {
                Name = taskName,
                DueDate = TaskDueDate.Date,
                Priority = SelectedPriority,
                IsCompleted = false
            };

            Tasks.Add(newTask);  // 添加任务

            // 直接将 Tasks 复制到 FilteredTasks
            UpdateFilteredTasks();

            TaskInput.Text = string.Empty; // 清空输入框
        }

        // 删除任务
        private void DeleteTask(TaskItem task)
        {
            Tasks.Remove(task);
            UpdateFilteredTasks();
        }

        // 编辑任务
        private void EditTask(TaskItem task)
        {
            TaskInput.Text = task.Name;
            TaskDueDate.Date = task.DueDate;
            SelectedPriority = task.Priority;
        }

        // 排序任务
        private void SortByDate()
        {
            var sortedTasks = Tasks.OrderBy(t => t.DueDate).ToList();
            Tasks.Clear();
            foreach (var task in sortedTasks)
            {
                Tasks.Add(task);
            }

            UpdateFilteredTasks();
        }

        // 筛选已完成的任务
        private void FilterCompleted()
        {
            var completedTasks = Tasks.Where(t => t.IsCompleted).ToList();
            FilteredTasks.Clear();
            foreach (var task in completedTasks)
            {
                FilteredTasks.Add(task);
            }
        }

        // 更新 FilteredTasks 的内容
        private void UpdateFilteredTasks()
        {
            FilteredTasks.Clear();
            foreach (var task in Tasks)
            {
                FilteredTasks.Add(task);
            }
        }
    }
}
